package jfae.core.properties.parsers;

import java.beans.BeanInfo;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.List;

import javax.persistence.Entity;

import jfae.core.annotations.Editable;
import jfae.core.annotations.Presentation;
import jfae.core.properties.FieldInfo;
import jfae.core.properties.impl.FieldInfoImpl;
import jfae.core.properties.parsers.annotation.AnnotationHandler;

public class FieldPropertyHandler extends AbstractAnnotationPropertyHandler<FieldInfo<?>>{
	

	public FieldPropertyHandler(List<AnnotationHandler<?>> annHandlers) {
		super(annHandlers);
	}

	public boolean accept(PropertyDescriptor prop, BeanInfo info){
		Method getter = prop.getReadMethod();
		Method setter = prop.getWriteMethod();
		return 
			getter != null && setter != null
			&& (
					getter.isAnnotationPresent(Editable.class)
					|| getter.isAnnotationPresent(Presentation.class)
			)
			&& !prop.getPropertyType().isAnnotationPresent(Entity.class);
	}
	

	/**
	 * This method is necessary for type safe creation of a generic {@link FieldInfo} object
	 * @param prop the property to wrap
	 * @param type the type of the property
	 * @return a {@link FieldInfo} instance 
	 */
	protected <T>FieldInfo<T> buildFieldInfo(PropertyDescriptor prop, Class<T> type) {
		return new FieldInfoImpl<T>(prop.getName(), type);
	}

}

